package main

import (
	"embed"
	"encoding/json"
	"fmt"
	"io"
	"log"
	"os"
)

var (
	g_app_config AppConfig
	APPSIZE      string

	//go:embed config/*
	fs embed.FS
)

type AppConfig struct {
	Version uint32 `json:"appversion"`
}

func main() {
	load_app_config()
	print_app_info()
	print_app_file()
}

func print_app_info() {
	fmt.Printf("version: %d\n", g_app_config.Version)
	fmt.Printf("Size:    %s\n", APPSIZE)
}

func load_app_config() {
	cn := "config/app.json"
	jsonFile, err := os.Open(cn)
	if err != nil {
		log.Fatal("failed to open file: ", cn, err)
	}
	defer jsonFile.Close()

	bs, err := io.ReadAll(jsonFile)
	if err != nil {
		log.Fatal("read error.", err)
	}

	if err := json.Unmarshal(bs, &g_app_config); err != nil {
		log.Fatal("parse json error.", err)
	}
}

func print_app_file() {
	fname := "config/app.json"
	content, err := fs.ReadFile(fname)
	if err != nil {
		log.Fatal("read file err: ", err)
	}

	fmt.Println(string(content))
}
